<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label">上货时间：</label>
                                    <div class="layui-input-inline">
                                        <input name="createTime" id="pTime" class="layui-input" type="text"
                                               placeholder="选择时间" autocomplete="off" style="width: 200px" readonly/>
                                    </div>
                                    <input name="startTime" id="startTime" type="hidden"/>
                                    <input name="endTime" id="endTime" type="hidden"/>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">上货单号：</label>
                                    <div class="layui-input-inline">
                                        <input name="code" class="layui-input" type="text" placeholder="请输入上货单号" autocomplete="off"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">部门：</label>
                                    <div class="layui-input-inline">
                                        <select  name="departmentId" lay-filter="departmentId"  lay-search lay-verify="" class="common-select-search">
                                            <option value="">请选择部门</option>
                                        </select>
                                    </div>
                                </div>

                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">操作人：</label>
                                    <div class="layui-input-inline">
                                        <select id="creatorId" name="creatorId" lay-filter="creatorId"  lay-search lay-verify="" class="common-select-search">
                                            <option value="">请选择操作人</option>
                                        </select>
                                    </div>
                                </div>

                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">柜子编号：</label>
                                    <div class="layui-input-inline">
                                        <select  name="cabCode" lay-filter="cabCode"  lay-search lay-verify="" class="common-select-search">
                                            <option value="">请选择柜子</option>
                                        </select>
                                    </div>
                                </div>



                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">货道编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="spaceCode" class="layui-input" type="text" placeholder="请输入货道编号" autocomplete="off"/>
                                    </div>
                                </div>

                               <!-- <div class="layui-inline">
                                    <label class="layui-form-label w-auto">上下货类型：</label>
                                    <div class="layui-input-inline">
                                        <select  name="loadType" lay-filter="loadType"  lay-search lay-verify="">
                                            <option value="">请选择上下货类型</option>
                                            <option value="load">上货</option>
                                            <option value="unload">下货</option>
                                        </select>
                                    </div>
                                </div>-->

                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">规格码：</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="skuSpecOld" class="layui-input" placeholder="请输入规格码">
                                       <!-- <select  name="skuCode4Query" lay-filter="skuCode4Query"  lay-search lay-verify="">
                                            <option value="">请选规格码</option>
                                        </select>-->
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">物料编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="skuCode" class="layui-input" type="text" placeholder="请输入物料编号" autocomplete="off"/>
                                    </div>
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit layui-form-keyDownSearch="true">查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="orderLoadBtnAdd" class="layui-btn icon-btn color-reseda">添加上货记录(按计划)</button>
                            <button id="orderLoadSimpleBtnAdd" class="layui-btn icon-btn color-reseda">添加自主上货记录</button>
                            <!--
                            <button id="orderUnloadSimpleBtnAdd" class="layui-btn icon-btn color-reseda">添加自主下货记录</button>
                            <button id="cellCabinetUnloadBtnAdd" class="layui-btn icon-btn color-reseda">添加格子柜下货记录</button>
                            <button id="orderBatchUnloadSimpleBtnAdd" class="layui-btn icon-btn color-reseda">添加批量自主下货记录</button>
                            -->

                        </div>
                        <div class="lay-btn-rt">
                            <button id="cellCabinetBtnAdd" class="layui-btn icon-btn color-green">添加格子柜上货记录</button>
                            <button id="orderBatchLoadSimpleBtnAdd" class="layui-btn icon-btn color-green">添加批量自主上货记录</button>
                            <button id="orderLoadBtnExport" class="layui-btn icon-btn color-green">导出</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="orderLoadTable" lay-filter="orderLoadTable"></table>
        </div>
    </div>
</div>

<style>
    .model-form .layui-form-select dl{
        max-height: 200px;
    }
</style>

<!-- 表格操作列 -->
<script type="text/html" id="orderLoadTableBar">
    <!--
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    -->
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">详情</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="orderLoadForm">
    <form lay-filter="orderLoadForm" class="layui-form model-form">
        <input name="id" type="hidden"/>

        <div class="layui-form-item">
            <label class="layui-form-label">上货类型</label>
            <div class="layui-input-block">
                <select name="type">
                        <option value="replenishment">补货上货</option>
                        <option value="selfLoad">自主上货</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">领货单</label>
            <div class="layui-input-block">
                <select name="receiveId" id="receiveId" lay-filter="receiveId" lay-verify="required" lay-search required></select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">配货需求单</label>
            <div class="layui-input-block">
                <input name="demandCode" type="text" class="layui-input" style="border: none" readonly/>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">上货计划单</label>
            <div class="layui-input-block">
                <input name="loadPlanCode" type="text" class="layui-input" style="border: none" readonly/>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">柜子</label>
            <div class="layui-input-block">
                <select name="cabId" id="cabId" lay-filter="cabId" lay-verify="required" lay-search required ></select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">货道</label>
            <div class="layui-input-block">
                <select name="spaceId" id="spaceId" lay-filter="spaceId" lay-verify="required" lay-search required></select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">物料</label>
            <div class="layui-input-block">
                <select name="skuId" id="skuId" lay-filter="skuId" lay-verify="required" lay-search required></select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">包装规格</label>
            <div class="layui-input-block">
                <select name="packId" id="packId" lay-filter="packId" lay-verify="required" lay-search required></select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">上货数量</label>
            <div class="layui-input-block">
                <input name="quantity" placeholder="请输入上货数量" type="text" class="layui-input" lay-verify="required|number|verifyQuantity" required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <input name="remark" placeholder="请输入备注" type="text" class="layui-input" lay-verify="" required/>
            </div>
        </div>



        <div class="layui-form-item text-right saveBtn">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="orderLoadFormSubmit" lay-submit>提交</button>
        </div>
    </form>
</script>


<script type="text/html" id="orderLoadSimpleForm">
    <form lay-filter="orderLoadSimpleForm" class="layui-form model-form">
        <input name="id" type="hidden"/>

        <div class="layui-form-item">
            <label class="layui-form-label">上货类型</label>
            <div class="layui-input-block">
                <select name="type">
                    <option value="selfLoadSimple">自主上货</option>
                </select>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">柜子</label>
            <div class="layui-input-block">
                <select name="cabId" id="cabId4simple" lay-filter="cabId" lay-verify="required" lay-search required ></select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">物料</label>
            <div class="layui-input-block">
                <select name="skuId" id="skuId4simple" lay-filter="skuId" lay-verify="required" lay-search required></select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">包装规格</label>
            <div class="layui-input-block">
                <select name="packId" id="packId4simple" lay-filter="packId" lay-verify="required" lay-search required></select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">货道</label>
            <div class="layui-input-block">
                <select name="spaceId" id="spaceId4simple" lay-filter="spaceId" lay-verify="required" lay-search required></select>
            </div>
        </div>



        <div class="layui-form-item">
            <label class="layui-form-label">上货数量</label>
            <div class="layui-input-block">
                <input name="quantity" placeholder="请输入上货数量" type="text" class="layui-input" lay-verify="required|number|verifyQuantity" required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <input name="remark" placeholder="请输入备注" type="text" class="layui-input" lay-verify="" required/>
            </div>
        </div>



        <div class="layui-form-item text-right saveSimpleBtn">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="orderLoadSimpleFormSubmit" lay-submit>提交</button>
        </div>
    </form>
</script>


<script type="text/html" id="orderUnloadSimpleForm">
    <form lay-filter="orderUnloadSimpleForm" class="layui-form model-form">
        <input name="id" type="hidden"/>




        <div class="layui-form-item">
            <label class="layui-form-label">柜子</label>
            <div class="layui-input-block">
                <select name="cabId" id="cabId4simpleUnload" lay-filter="cabId" lay-verify="required" lay-search required ></select>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">货道</label>
            <div class="layui-input-block">
                <select name="spaceId" id="spaceId4simpleUnload" lay-filter="spaceId" lay-verify="required" lay-search required></select>
            </div>
        </div>



        <div class="layui-form-item">
            <label class="layui-form-label">下货数量</label>
            <div class="layui-input-block">
                <input name="quantity" placeholder="请输入下货数量" type="text" class="layui-input" lay-verify="required|number|verifyQuantity" required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <input name="remark" placeholder="请输入备注" type="text" class="layui-input" lay-verify="" required/>
            </div>
        </div>



        <div class="layui-form-item text-right saveUnloadSimpleBtn">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="orderUnloadSimpleFormSubmit" lay-submit>提交</button>
        </div>
    </form>
</script>


<script type="text/html" id="orderBatchLoadSimpleForm">
    <form lay-filter="orderBatchLoadSimpleForm" class="layui-form model-form">
        <input name="id" type="hidden"/>

        <div class="layui-form-item">
            <label class="layui-form-label">上货类型</label>
            <div class="layui-input-block">
                <select name="type">
                    <option value="selfBatchLoadSimple">自主上货</option>
                </select>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">柜子</label>
            <div class="layui-input-block">
                <select name="cabId" id="cabId4BatchSimple" lay-filter="cabId" lay-verify="required" lay-search required ></select>
            </div>
        </div>
        <!--
        <div class="layui-form-item">
            <label class="layui-form-label">物料</label>
            <div class="layui-input-block">
                <select name="skuId" id="skuId4BatchSimple" lay-filter="skuId" lay-verify="required" lay-search required></select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">货道</label>
            <div class="layui-input-block">
                <select name="spaceId" id="spaceId4BatchSimple" lay-filter="spaceId" lay-verify="required" lay-search required></select>
            </div>
        </div>



        <div class="layui-form-item">
            <label class="layui-form-label">上货数量</label>
            <div class="layui-input-block">
                <input name="quantity" placeholder="请输入上货数量" type="text" class="layui-input" lay-verify="required|number|verifyQuantity" required/>
            </div>
        </div>
        -->
        <div class="layui-form-item">
            <label class="layui-form-label">上货明细</label>
            <div class="layui-input-block">
                <table class="layui-table" id="cabLaneTable" lay-filter="cabLaneTable"></table>
            </div>
        </div>

        <div class="layui-form-item" id="cabLaneBtnAddDiv">
            <label class="layui-form-label"></label>
            <div class="layui-input-inline">
                <button id="cabLaneBtnAdd" class="layui-btn" lay-filter="cabLaneBtnAdd" onclick="return false;">增加上货明细</button>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <input name="remark" placeholder="请输入备注" type="text" class="layui-input" lay-verify="" required/>
            </div>
        </div>



        <div class="layui-form-item text-right saveBatchSimpleBtn">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="orderBatchLoadSimpleFormSubmit" lay-submit>提交</button>
        </div>
    </form>
</script>


<script type="text/html" id="orderBatchUnloadSimpleForm">
    <form lay-filter="orderBatchUnloadSimpleForm" class="layui-form model-form">
        <input name="id" type="hidden"/>




        <div class="layui-form-item">
            <label class="layui-form-label">柜子</label>
            <div class="layui-input-block">
                <select name="cabId" id="cabId4simpleBatchUnload" lay-filter="cabId" lay-verify="required" lay-search required ></select>
            </div>
        </div>

        <!--
        <div class="layui-form-item">
            <label class="layui-form-label">货道</label>
            <div class="layui-input-block">
                <select name="spaceId" id="spaceId4simpleBatchUnload" lay-filter="spaceId" lay-verify="required" lay-search required></select>
            </div>
        </div>



        <div class="layui-form-item">
            <label class="layui-form-label">下货数量</label>
            <div class="layui-input-block">
                <input name="quantity" placeholder="请输入下货数量" type="text" class="layui-input" lay-verify="required|number|verifyQuantity" required/>
            </div>
        </div>
        -->

        <div class="layui-form-item">
            <label class="layui-form-label">下货明细</label>
            <div class="layui-input-block">
                <table class="layui-table" id="cabLaneUnloadTable" lay-filter="cabLaneUnloadTable"></table>
            </div>
        </div>

        <div class="layui-form-item" id="cabLaneUnloadBtnAddDiv">
            <label class="layui-form-label"></label>
            <div class="layui-input-inline">
                <button id="cabLaneUnloadBtnAdd" class="layui-btn" lay-filter="cabLaneUnloadBtnAdd" onclick="return false;">增加上货明细</button>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <input name="remark" placeholder="请输入备注" type="text" class="layui-input" lay-verify="" required/>
            </div>
        </div>



        <div class="layui-form-item text-right saveBatchUnloadSimpleBtn">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="orderBatchUnloadSimpleFormSubmit" lay-submit>提交</button>
        </div>
    </form>
</script>

<!-- 格子柜上货 -->
<script type="text/html" id="cellCabinetForm">
    <form lay-filter="cellCabinetForm" class="layui-form model-form">
        <input name="id" type="hidden"/>

        <div class="layui-form-item">
            <label class="layui-form-label">上货类型</label>
            <div class="layui-input-block">
                <select name="type">
                    <option value="selfLoadSimple">自主上货</option>
                </select>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">柜子</label>
            <div class="layui-input-block">
                <select name="cabId" id="cellCabId" lay-filter="cabId" lay-verify="required" lay-search required ></select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">物料</label>
            <div class="layui-input-block">
                <select name="skuId" id="productId" lay-filter="skuId" lay-verify="required" lay-search required></select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">包装规格</label>
            <div class="layui-input-block">
                <select name="packId" id="packId4Space" lay-filter="packId" lay-verify="required" lay-search required></select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">货道</label>
            <div class="layui-input-block">
                <select name="spaceId" id="cellSpaceId" lay-filter="spaceId" lay-verify="required" lay-search required></select>
            </div>
        </div>



        <div class="layui-form-item">
            <label class="layui-form-label">上货数量</label>
            <div class="layui-input-block">
                <input name="quantity" placeholder="请输入上货数量" type="text" class="layui-input" lay-verify="required|number|verifyQuantity" required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <input name="remark" placeholder="请输入备注" type="text" class="layui-input" lay-verify="" required/>
            </div>
        </div>



        <div class="layui-form-item text-right saveSimpleBtn">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="cellCabinetFormSubmit" lay-submit>提交</button>
        </div>
    </form>
</script>

<!-- 格子柜下货 -->
<script type="text/html" id="cellCabinetUnloadForm">
    <form lay-filter="cellCabinetUnloadForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">柜子</label>
            <div class="layui-input-block">
                <select name="cabId" id="cabIdUnload" lay-filter="cabId" lay-verify="required" lay-search required ></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">货道</label>
            <div class="layui-input-block">
                <select name="spaceId" id="spaceIdUnload" lay-filter="spaceId" lay-verify="required" lay-search required></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">下货数量</label>
            <div class="layui-input-block">
                <input name="quantity" placeholder="请输入下货数量" type="text" class="layui-input" lay-verify="required|number|verifyQuantity" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <input name="remark" placeholder="请输入备注" type="text" class="layui-input" lay-verify="" required/>
            </div>
        </div>
        <div class="layui-form-item text-right saveUnloadSimpleBtn">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="cellCabinetUnloadFormSubmit" lay-submit>提交</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laydate = layui.laydate;

        admin.selectSearch();

        // 渲染表格
        var insTb = table.render({
            elem: '#orderLoadTable',
            // url: config.base + 'enterprisecab/order/admin/orderReceiveLoad/list',
            url: config.base + 'enterprisecab/order/admin/orderReceiveLoadDetail/list?loadType=load',
            toolbar: true,
            defaultToolbar: ["filter"],
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers',title: '序号', fixed: 'left'}
                ,{field: 'skuName', title: '物料名称', fixed: 'left', width: 160}
                ,{field: 'skuCode', sort: true, title: '物料编号', fixed: 'left', width: 160}
                ,{field: 'skuSpecOld', title: '规格码', fixed: 'left', width: 160}
                ,{field: 'productBrandName', title: '品牌'}
                ,{field: 'code', title: '上货单号', width: 160}
                , {field: 'departmentName', sort: false, title: '部门'}
                , {field: 'costCenter', sort: false, title: '成本中心'}
                // ,{field: 'typeDesc', sort: true, title: '上货类型'}
                // ,{field: 'demandCode', sort: true, title: '配货需求单'}
                // ,{field: 'loadPlanCode', sort: true, title: '上/下货计划单'}
                ,{field: 'cabCode', sort: true, title: '柜子编号', width: 130}
                ,{field: 'cabName', title: '柜子名称'}
                ,{field: 'spaceCode', sort: true, title: '货道编号'}
                ,{field: 'packName', sort: true, title: '包装规格'}
                ,{field: 'quantity', sort: true, title: '数量(包装计)', width: 120}
                ,{field: 'unitName', sort: true, title: '包装单位'}
                ,{field: 'quantityPack', sort: true, title: '数量(基础单位计)', width: 150}
                , {field: 'basicsUnitName', sort: true, title: '基础单位', templet: d=> d.basicsUnitName ? d.basicsUnitName : '无'}
                ,{field: 'creatorName', sort: true, title: '上货人', width: 120}
                ,{field: 'createTime', sort: true, title: '上货时间', width: 160}
                // ,{align: 'center', toolbar: '#orderLoadTableBar', title: '操作', width: 80, fixed: 'right'}
            ]],
        });

        // 工具条点击事件
        table.on('tool(orderLoadTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                data.eventType='edit';//selfLoadSimple
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }else if(obj.event=='detail'){
                data.eventType='detail';
                if(data.type=='selfLoadSimple' || data.type=='selfLoad'){
                    showEditLoadSimpleModel(data);
                }else if(data.type=='selfUnloadSimple'){
                    showEditUnloadSimpleModel(data);
                }else if(data.type=='selfBatchLoadSimple'){
                    showEditBatchLoadSimpleModel(data);
                }else if(data.type=='selfBatchUnloadSimple'){
                    showEditBatchUnloadSimpleModel(data);
                }else{
                    showEditModel(data);
                }

            }
        });

        //监听排序
        table.on('sort(orderLoadTable)', function(obj){
            table.reload('orderLoadTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        form.verify({
            verifyQuantity: function (value) {
                var bool = false;
                var capacity=$("#spaceId option:selected").data('storagecapacity');
                if(parseInt(value)>parseInt(capacity)){
                    bool=true;
                }
                if(bool){
                    return "输入数量不能大于货道最大可装填数量："+capacity;
                }
            }
        });



        // 获取部门
        admin.req('user/admin/department/list', {limit:999}, function (res) {

            if (200 == res.code) {
                var optionHtml='<option value=""> 请选择部门</option>';
                for(var k=0;k<res.data.length;k++){
                    optionHtml+='<option value="'+res.data[k].id+'">'+res.data[k].name+'</option>';
                }
                $("select[name='departmentId']").html(optionHtml);
                form.render('select');

            } else {
                layer.msg('获取部门失败', {icon: 2, time: 1000});
            }
        }, 'GET');

        var selectManager=function(departmentId,elementId,optionDesc){
            //获取责任人erpDepartmentId
            admin.req('user/admin/user/list', {departmentId:departmentId,limit:999}, function (res) {
                if (200 == res.code) {
                    var optionHtml='<option value="">请选择'+optionDesc+'</option>';
                    for(var k=0;k<res.data.length;k++){
                        optionHtml+='<option value="'+res.data[k].id+'">'+res.data[k].nickname+'|'+res.data[k].username+'</option>';
                    }
                    $(elementId).html(optionHtml);
                    form.render('select');


                } else {
                    layer.msg('获取责任人失败', {icon: 2, time: 1000});
                }
            }, 'GET');
        }


        form.on('select(departmentId)',function(data){
            selectManager(data.value,'#creatorId','操作人');
        })

        admin.req('enterprisecab/cabinet/admin/cabinet/list', {limit:999,cabinetNature:'sale',cabinetType:'spring'}, function (res) {
            if (200 == res.code) {
                var optionHtml='<option value="">请选择柜子</option>';
                for(var k=0;k<res.data.length;k++){
                    optionHtml+='<option value="'+res.data[k].code+'">'+res.data[k].code+'</option>';
                }
                $("select[name='cabCode']").html(optionHtml);
                form.render('select')
            } else {
                layer.msg(res.msg, {icon: 2, time: 1000});
            }
        }, 'GET');

        admin.req('enterprisecab/cabProduct/admin/wMostUsedKnifeList/list', {limit:999}, function (res) {

            if (200 == res.code) {
                var optionHtml='<option value="">请选择规格</option>';
                for(var k=0;k<res.data.length;k++){
                    optionHtml+='<option value="'+res.data[k].skuCode+'">'+res.data[k].oldSpecCode+'</option>';
                }
                $("select[name='skuCode4Query']").html(optionHtml);
                form.render('select');

            } else {
                layer.msg('获取物料失败', {icon: 2, time: 1000});
            }
        }, 'GET');

        form.render('select');



        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
        });

        form.on('submit(resets)', function (data) {
            //
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            var field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
            $('#creatorId').html('');
            form.render();
        });

        // 添加按钮点击事件
        $('#orderLoadBtnAdd').click(function () {
            showEditModel();
        });

        $('#orderLoadSimpleBtnAdd').click(function () {
            showEditLoadSimpleModel();
        });

        $('#orderUnloadSimpleBtnAdd').click(function () {
            showEditUnloadSimpleModel();
        });

        $('#orderBatchLoadSimpleBtnAdd').click(function () {
            showEditBatchLoadSimpleModel();
        });

        $('#orderBatchUnloadSimpleBtnAdd').click(function () {
            showEditBatchUnloadSimpleModel();
        });

        //格子柜上货
        $('#cellCabinetBtnAdd').click(function () {
            showCellCabinetModel();
        });

        //格子柜下货
        $('#cellCabinetUnloadBtnAdd').click(function () {
            showEditCellCabinetUnloadSimpleModel();
        });
        
        function showEditUnloadSimpleModel(data) {
            console.log(data)
            var title='添加自主下货';
            if(data){
                title='自主下货详情'
            }
            admin.open({
                type: 1,
                area: '700px',
                offset: '65px',
                title: title,
                content: $('#orderUnloadSimpleForm').html(),
                success: function () {
                    if (data){
                        var option = $("form select[name='type']").find("option");
                        layui.each(option, function(index, item){
                            if(data.type  == item.text){
                                data.type = item.value;
                            }
                        });
                    }




                    admin.req('enterprisecab/cabinet/admin/cabinet/list', {cabinetNature:'sale',limit:999}, function (res) {

                        if (res.code == 200) {
                            var html='<option value="">请选择售刀柜</option>'
                            for(var k=0;k<res.data.length;k++){
                                html+='<option value="'+res.data[k].id+'" >'+res.data[k].code+'</option>'
                            }
                            $("#cabId4simpleUnload").html(html);
                            form.render('select');
                            if(data){
                                $("#cabId4simpleUnload").val(data.cabId);
                                form.render('select');
                                getCabSpace(data.cabId);
                            }
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GEt');



                    var getCabSpace=function(cabinetId,skuCode){

                        var queryFlag='unloadSimple';
                        if(data&&data.eventType=='detail'){
                            queryFlag='unloadSimple4Detail';
                        }
                        admin.req('enterprisecab/cabinet/admin/cabinetCargoLane/list', {cabinetId:cabinetId,limit:999,queryFlag:queryFlag}, function (res) {

                            if (res.code == 200) {
                                var html='<option value="">请选择货道</option>'
                                for(var k=0;k<res.data.length;k++){
                                    html+='<option value="'+res.data[k].id+'" data-cabcode="'+res.data[k].cabCode+'" data-skucode="'+res.data[k].skuCode+'" data-skuid="'+res.data[k].skuId+'" data-storagecapacity="'+res.data[k].storageCapacity+'" data-currentstock="'+res.data[k].currentStockCount+'">'+res.data[k].code+'</option>'
                                }
                                $("#spaceId4simpleUnload").html(html);
                                form.render('select');
                                if(data){
                                    $("#spaceId4simpleUnload").val(data.spaceId);
                                    form.render('select');

                                    // var skuCode=$("#spaceId option:selected").data('skucode');
                                    // var skuId=$("#spaceId option:selected").data('skuid');
                                    // var capacity=$("#spaceId4simple option:selected").data('storagecapacity')
                                    // if (skuCode!=''&&skuCode!=null){
                                    //     var html='<option value="'+skuId+'" >'+skuCode+'</option>'
                                    //     $("#skuId").html(html);
                                    //     form.render('select');
                                    //     $("#skuId").val(data.skuId);
                                    //     form.render('select');
                                    // }else{
                                    //     getProduct(data.spaceId);
                                    // }
                                }
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GEt');
                    }

                    form.on('select(cabId)',function(selectData){
                        var cabCode=$(this).find("option:selected").text();
                        if(selectData.value!=''){
                            getCabSpace(selectData.value);
                        }
                    })






                    form.on('select(spaceId)',function(selectData){

                        // var skuCode=$("#spaceId option:selected").data('skucode');
                        // var skuId=$("#spaceId option:selected").data('skuid');
                        var capacity=$("#spaceId4simpleUnload option:selected").data('currentstock')
                        $("form input[name='quantity']").attr('placeholder','货道最大可下货数量：'+capacity);
                        // if(selectData.value!=''){
                        //     if (skuCode!=''&&skuCode!=null){
                        //         var html='<option value="'+skuId+'" >'+skuCode+'</option>'
                        //         $("#skuId").html(html);
                        //         form.render('select');
                        //     }else{
                        //         getProduct(selectData.value);
                        //     }
                        //
                        // }
                    })

                    if(data&&data.eventType=='detail'){
                        $(".saveUnloadSimpleBtn").find('*').hide();
                        $(".model-form").find('*').attr('disabled','disabled');
                        $(".model-form input").attr('readonly',true);
                        $(".model-form select").attr('readonly',true);
                    }

                    form.val('orderUnloadSimpleForm', data);
                    // 表单提交事件
                    form.on('submit(orderUnloadSimpleFormSubmit)', function (d) {
                        layer.load(2);
                        d.field.cabCode=$("#cabId4simpleUnload option:selected").text();
                        d.field.spaceCode=$("#spaceId4simpleUnload option:selected").text();
                        d.field.skuCode=$("#spaceId4simpleUnload option:selected").data('skucode');
                        d.field.type='selfUnloadSimple';
                        admin.req(data ? 'enterprisecab/order/admin/orderReceiveLoad/update' : 'enterprisecab/order/admin/orderReceiveLoad/unload', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('orderLoadTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }


        function showEditBatchUnloadSimpleModel(data) {
            var title='添加批量自主下货';
            if(data){
                title='批量自主下货详情'
            }
            admin.open({
                type: 1,
                area: '1000px',
                offset: '65px',
                title: title,
                content: $('#orderBatchUnloadSimpleForm').html(),
                success: function () {
                    if (data){
                        var option = $("form select[name='type']").find("option");
                        layui.each(option, function(index, item){
                            if(data.type  == item.text){
                                data.type = item.value;
                            }
                        });
                    }

                    var cabLaneHtml='';
                    var getCabSpace=function(cabinetId,skuCode){

                        var queryFlag='unloadSimple';
                        if(data&&data.eventType=='detail'){
                            queryFlag='unloadSimple4Detail';
                        }
                        admin.req('enterprisecab/cabinet/admin/cabinetCargoLane/list', {cabinetId:cabinetId,limit:999,queryFlag:queryFlag}, function (res) {

                            if (res.code == 200) {
                                var html='<option value="">请选择货道</option>'
                                for(var k=0;k<res.data.length;k++){
                                    html+='<option value="'+res.data[k].id+'" data-cabcode="'+res.data[k].cabCode+'" data-skucode="'+res.data[k].skuCode+'" data-skuid="'+res.data[k].skuId+'" data-storagecapacity="'+res.data[k].storageCapacity+'" data-currentstock="'+res.data[k].currentStockCount+'">'+res.data[k].code+'</option>'
                                }
                                cabLaneHtml=html;

                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GEt',false);
                    }


                    admin.req('enterprisecab/cabinet/admin/cabinet/list', {cabinetNature:'sale',limit:999}, function (res) {

                        if (res.code == 200) {
                            var html='<option value="">请选择售刀柜</option>'
                            for(var k=0;k<res.data.length;k++){
                                html+='<option value="'+res.data[k].id+'" >'+res.data[k].code+'</option>'
                            }
                            $("#cabId4simpleBatchUnload").html(html);
                            form.render('select');
                            if(data){
                                $("#cabId4simpleBatchUnload").val(data.cabId);
                                form.render('select');
                                getCabSpace(data.cabId);
                            }
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GEt',false);




                    form.on('select(cabId)',function(selectData){

                        if(selectData.value!=''){
                            getCabSpace(selectData.value);
                            $("form select[name='cabLaneId']").each(function(index,item){
                                var id=item.id;

                                var eleNo=id.substring('cabLaneId'.length,id.length);
                                // var skuId=$(this).val();
                                // var skuCode=$(this).find('option:selected').data('skucode');
                                // var cabLaneId=$("#cabLaneId"+eleNo).val();
                                $("#cabLaneId"+eleNo).html(cabLaneHtml);
                                $("#quantity"+eleNo).attr('placeholder','请输入数量');
                                form.render();

                            })
                        }
                    })





                    var j=0;





                    var tHeaderHtml='<thead id="cabLaneUnloadHead">' +
                        '<tr>' +
                        '<th>序号</th>' +
                        '<th>货道</th>' +
                        '<th>数量</th>' ;
                    if(data==undefined||(data&&data.eventType!='audit'&&data.eventType!='detail')){
                        tHeaderHtml+='<th><div align="center">操作</div></th>' ;
                    }

                    tHeaderHtml+='</tr>' +
                        '</thead>';
                    var html=tHeaderHtml+"<tbody id='cabLaneUnloadTbody'></tbody>";
                    $("#cabLaneUnloadTable").html(html);
                    form.render();


                    var getCabLaneHtml=function(j){
                        var html= '                <select id="cabLaneId'+j+'"  name="cabLaneId" lay-filter="cabLaneId'+j+'"  lay-verify="required" lay-search >\n' +
                            cabLaneHtml+
                            '                </select>\n' ;
                        return html;
                    }


                    var getQuantityHtml=function(j){
                        var html='<input id="quantity'+j+'" name="quantity" placeholder="请输入数量" type="text" class="layui-input" lay-verify="required|number" required/>' ;
                        return html;
                    }


                    var addTableTr=function(resultData){

                        var tBodyHtml='<tr class="layui-form cabLaneTr" id="cabLaneTr'+j+'">' +
                            '<td>'+(j+1)+'</td>' +
                            '<td style="table-layout: fixed" id="cabLaneTd'+j+'">'+getCabLaneHtml(j)+'</td>' +

                            '<td id="quantityTd'+j+'">'+getQuantityHtml(j)+'</td>';
                        if(data==undefined||(data&&data.eventType!='audit'&&data.eventType!='detail')){
                            tBodyHtml+='<td style="width: 50px;table-layout: fixed" class="layui-table-cell"  align="center">' +
                                '<button id="delBtn' + j + '" class="layui-btn layui-btn-xs delBtn" type="button" lay-filter="delBtn' + j + '"  formnovalidate>删除</button>'+
                                '</td>';
                        }

                        tBodyHtml+='</tr>' ;

                        var $tr=$("#cabLaneUnloadTbody tr")
                        if($tr.length==0){
                            $("#cabLaneUnloadTbody").html(tBodyHtml);
                        }else{
                            $($tr[$tr.length-1]).after(tBodyHtml)
                            // $tr[$tr.length-1].after(tBodyHtml);
                            form.render();
                        }
                        form.render();

                        form.on('select(cabLaneId'+j+')',function(data){
                            if(data.value!=''){
                                var selectDom=$(this);
                                var parentDom=selectDom.parent().parent().parent();
                                var selectHtml=parentDom[0].innerHTML;
                                var startIndex=selectHtml.indexOf('cabLaneId',0);
                                var endIndex=selectHtml.indexOf('"',startIndex);
                                var selectDomNo=selectHtml.substring(startIndex,endIndex);
                                var selectDomNo=selectDomNo.substring("cabLaneId".length,selectDomNo.length);
                                var capacity=$("#cabLaneId"+selectDomNo+" option:selected").data('currentstock')
                                $("#quantity"+selectDomNo).attr('placeholder','货道最大可下货数量：'+capacity);
                                // var capacity=$("#cabLaneId"+selectDomNo+" option:selected").data('storagecapacity')
                                // $("form input[name='quantity']").attr('placeholder','货道最大可下货数量：'+capacity);
                            }
                        })

                        j++;
                        $(".delBtn").off('click').on('click',function(data){
                            var btn=$(this);
                            var btnId=btn[0].id;
                            var btnNo=btnId.substring("delBtn".length,btnId.length);

                            layer.confirm('确定要删除吗？', {
                                offset: '65px',
                                skin: 'layui-layer-admin'
                            }, function (i) {
                                layer.close(i);
                                btn.parent().parent().remove()
                                // layer.load(2);

                            });
                        })

                    }

                    var getLoadDetail=function(){
                        admin.req('enterprisecab/order/admin/orderReceiveLoadDetail/list', {loadId:data.id,limit:9999}, function (res) {
                            console.log(data);
                            if (res.code==200) {
                                for(let k=0;k<res.data.length;k++){
                                    addTableTr();
                                    var selectDomNo=j-1;
                                    var spaceId=res.data[k].spaceId;
                                    var quantity=res.data[k].quantity;

                                    $("#quantity"+selectDomNo).val(quantity);
                                    form.render('select')

                                    $("#cabLaneId"+selectDomNo).val(spaceId);
                                    form.render('select')
                                }
                                if(data&&(data.eventType=='audit'||data.eventType=='detail')){
                                    $("#cabLaneUnloadTbody").find("*").attr('disabled','disabled');
                                    form.render();
                                }
                            } else {
                                layer.msg('获取明细失败', {icon: 2, time: 1000});
                            }
                        }, 'GET');
                    }

                    $("#cabLaneUnloadBtnAdd").click(addTableTr);
                    if(data==undefined){
                        addTableTr();
                    }else{
                        getLoadDetail();
                        $(".saveBatchUnloadSimpleBtn").hide();
                        $("#cabLaneUnloadBtnAddDiv").hide();
                    }




                    form.val('orderUnloadSimpleForm', data);
                    // 表单提交事件
                    form.on('submit(orderBatchUnloadSimpleFormSubmit)', function (d) {
                        // layer.load(2);
                        d.field.cabCode=$("#cabId4simpleBatchUnload option:selected").text();
                        var detailList=[];
                        var detailMap=new Map();
                        var spaceSkuMap=new Map();
                        $("form select[name='cabLaneId']").each(function(index,item){
                            var id=item.id;
                            // var id2=this.id;
                            var eleNo=id.substring('cabLaneId'.length,id.length);


                            var cabLaneId=$("#cabLaneId"+eleNo).val();
                            var cabLaneCode=$("#cabLaneId"+eleNo+" option:selected").text();
                            var quantity=$("#quantity"+eleNo).val();

                            var currentstock=$("#cabLaneId"+eleNo+" option:selected").data('currentstock')
                            var skuId=$("#cabLaneId"+eleNo+" option:selected").data('skuid')
                            var skuCode=$("#cabLaneId"+eleNo+" option:selected").data('skucode')
                            detailList.push({skuCode:skuCode,skuId:skuId,spaceId:cabLaneId,spaceCode:cabLaneCode,quantity:quantity,cabId:d.field.cabId,cabCode:d.field.cabCode})

                            var val2=spaceSkuMap.get(cabLaneCode);
                            if(val2){
                                val2.quantity+=parseInt(quantity);
                                spaceSkuMap.set(cabLaneCode,val2);
                            }else{
                                spaceSkuMap.set(cabLaneCode,{cabLaneCode:cabLaneCode,currentstock:parseInt(currentstock),quantity:parseInt(quantity)});
                            }

                        })
                        if(detailList.length==0){
                            layer.msg('请增加上货明细', {icon: 6, time: 2000});
                            return false;
                        }

                        var keys2=[...spaceSkuMap.keys()]
                        var quantityObj
                        keys2.forEach(item=>{
                            var val=spaceSkuMap.get(item);
                            if(val.quantity>val.currentstock){
                                quantityObj=val;
                            }
                        })
                        if(quantityObj){
                            layer.msg('货道['+quantityObj.cabLaneCode+']下货数量['+quantityObj.quantity+']不能大于当前货道库存['+quantityObj.currentstock+']', {icon: 6, time: 2000});
                            return false;
                        }



                        d.field.detailList=detailList;
                        // d.field.spaceCode=$("#spaceId4simpleUnload option:selected").text();
                        // d.field.skuCode=$("#spaceId4simpleUnload option:selected").data('skucode');
                        d.field.type='selfBatchUnloadSimple';
                        admin.req(data ? 'enterprisecab/order/admin/orderReceiveLoad/update' : 'enterprisecab/order/admin/orderReceiveLoad/batchUnload', JSON.stringify(d.field), function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('orderLoadTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST',true,'application/json; charset=utf-8');
                        return false;
                    });
                }
            });
        }

        function showEditLoadSimpleModel(data) {
            var title='';
            if(data==undefined){
                title='添加自主上货';
            }else if(data&&data.eventType=='detail'){
                title='添加自主上货详情';
            }
            admin.open({
                type: 1,
                area: '900px',
                offset: '65px',
                title: title,
                content: $('#orderLoadSimpleForm').html(),
                success: function () {
                    if (data){
                        var option = $("form select[name='type']").find("option");
                        layui.each(option, function(index, item){
                            if(data.type  == item.text){
                                data.type = item.value;
                            }
                        });
                    }




                    admin.req('enterprisecab/cabinet/admin/cabinet/list', {cabinetNature:'sale',limit:999,cabinetType:'spring'}, function (res) {

                        if (res.code == 200) {
                            var html='<option value="">请选择售刀柜</option>'
                            for(var k=0;k<res.data.length;k++){
                                html+='<option value="'+res.data[k].id+'" >'+res.data[k].code+'</option>'
                            }
                            $("#cabId4simple").html(html);
                            form.render('select');
                            if(data){
                                $("#cabId4simple").val(data.cabId);
                                form.render('select');
                                getCabSpace(data.cabId);
                            }
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GEt');

                    var getProduct=function(){
                        // var receiveId=$("#receiveId").val();

                        admin.req('enterprise/product/admin/productDetail/list', {limit:999}, function (res) {

                            if (res.code == 200) {
                                var html='<option value="">请选择物料</option>'
                                for(var k=0;k<res.data.length;k++){
                                    html+='<option value="'+res.data[k].id+'"data-skucode="'+res.data[k].skuCode+'">'+'物料编号:'+res.data[k].skuCode+'|物料名称:'+res.data[k].skuName+'|规格:'+res.data[k].oldSpecCode+'</option>'
                                }
                                $("#skuId4simple").html(html);
                                form.render('select');
                                if(data){
                                    $("#skuId4simple").val(data.skuId);
                                    form.render('select');
                                    // getCabSpace(data.cabId,data.skuCode);
                                }
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GEt');
                    }

                    getProduct();

                    admin.req('enterprise/product/admin/productPack/list', {limit:999,sort:'id',order:'asc'}, function (res) {

                        if (res.code == 200) {
                            var html=''
                            for(var k=0;k<res.data.length;k++){
                                html+='<option value="'+res.data[k].id+'" data-unitname="'+res.data[k].productUnitName+'" data-quantitypackunit="'+res.data[k].baseRate+'">'+res.data[k].name+'</option>'
                            }
                            $("#packId4simple").html(html);
                            form.render('select');
                            if(data){
                                $("#packId4simple").val(data.packId);
                                form.render('select');
                                // getCabSpace(data.cabId,data.skuCode);
                            }
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GEt');

                    var getCabSpace=function(cabinetId,skuCode,quantityPackUnit){

                        var queryFlag='loadSimple';
                        if(data&&data.eventType=='detail'){
                            queryFlag='loadSimple4Detail';
                        }
                        admin.req('enterprisecab/cabinet/admin/cabinetCargoLane/list', {cabinetId:cabinetId,skuCode:skuCode,quantityPackUnit:quantityPackUnit,limit:999,queryFlag:queryFlag}, function (res) {

                            if (res.code == 200) {
                                var html='<option value="">请选择货道</option>'
                                for(var k=0;k<res.data.length;k++){
                                    html+='<option value="'+res.data[k].id+'" data-cabcode="'+res.data[k].cabCode+'" data-skucode="'+res.data[k].skuCode+'" data-skuid="'+res.data[k].skuId+'" data-storagecapacity="'+res.data[k].storageCapacity+'">'+res.data[k].code+'</option>'
                                }
                                $("#spaceId4simple").html(html);
                                form.render('select');
                                if(data){
                                    $("#spaceId4simple").val(data.spaceId);
                                    form.render('select');

                                    // var skuCode=$("#spaceId option:selected").data('skucode');
                                    // var skuId=$("#spaceId option:selected").data('skuid');
                                    // var capacity=$("#spaceId4simple option:selected").data('storagecapacity')
                                    // if (skuCode!=''&&skuCode!=null){
                                    //     var html='<option value="'+skuId+'" >'+skuCode+'</option>'
                                    //     $("#skuId").html(html);
                                    //     form.render('select');
                                    //     $("#skuId").val(data.skuId);
                                    //     form.render('select');
                                    // }else{
                                    //     getProduct(data.spaceId);
                                    // }
                                }
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GEt');
                    }

                    form.on('select(cabId)',function(selectData){
                        var cabCode=$(this).find("option:selected").text();
                        var quantityPackUnit=$(this).data('quantitypackunit');
                        var skuCode=$("#skuId4simple option:selected").data('skucode');
                        if(selectData.value!=''&&quantityPackUnit!=''&&skuCode!=''){
                            getCabSpace(selectData.value,skuCode,quantityPackUnit);
                        }
                    })

                    form.on('select(packId)',function(selectData){
                        var cabId=$("#cabId4simple option:selected").val();
                        var quantityPackUnit=$("#packId4simple option:selected").data('quantitypackunit');
                        var skuCode=$("#skuId4simple option:selected").data('skucode');
                        if(selectData.value!=''&&skuCode!=''&&cabId!=''){
                            getCabSpace(cabId,skuCode,quantityPackUnit);
                        }
                    })





                    form.on('select(skuId)',function (selectData) {
                        var cabId=$("#cabId4simple").val();
                        var skuCode=$("#skuId4simple option:selected").data('skucode');
                        var quantityPackUnit=$("#packId4simple option:selected").data('quantitypackunit');
                        if(selectData.value!=''&&cabId!=''&&quantityPackUnit!=''){
                            getCabSpace(cabId,skuCode,quantityPackUnit);
                        }
                    })

                    form.on('select(spaceId)',function(selectData){

                        // var skuCode=$("#spaceId option:selected").data('skucode');
                        // var skuId=$("#spaceId option:selected").data('skuid');
                        var capacity=$("#spaceId4simple option:selected").data('storagecapacity')
                        $("form input[name='quantity']").attr('placeholder','货道最大可装填容量：'+capacity);
                        // if(selectData.value!=''){
                        //     if (skuCode!=''&&skuCode!=null){
                        //         var html='<option value="'+skuId+'" >'+skuCode+'</option>'
                        //         $("#skuId").html(html);
                        //         form.render('select');
                        //     }else{
                        //         getProduct(selectData.value);
                        //     }
                        //
                        // }
                    })

                    if(data&&data.eventType=='detail'){
                        $(".saveSimpleBtn").find('*').hide();
                        $(".model-form").find('*').attr('disabled','disabled');
                        $(".model-form input").attr('readonly',true);
                        $(".model-form select").attr('readonly',true);
                    }

                    form.val('orderLoadSimpleForm', data);
                    // 表单提交事件
                    form.on('submit(orderLoadSimpleFormSubmit)', function (d) {
                        layer.load(2);
                        d.field.cabCode=$("#cabId4simple option:selected").text();
                        d.field.spaceCode=$("#spaceId4simple option:selected").text();
                        d.field.skuCode=$("#skuId4simple option:selected").data('skucode');
                        d.field.packName=$("#packId4simple option:selected").text();
                        d.field.unitName=$("#packId4simple option:selected").data('unitname');
                        d.field.quantityPackUnit=$("#packId4simple option:selected").data('quantitypackunit');
                        admin.req(data ? 'enterprisecab/order/admin/orderReceiveLoad/update' : 'enterprisecab/order/admin/orderReceiveLoad/loadSimple', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('orderLoadTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        function showEditBatchLoadSimpleModel(data) {
            var title='';
            if(data==undefined){
                title='添加批量自主上货';
            }else if(data&&data.eventType=='detail'){
                title='批量自主上货详情';
            }
            admin.open({
                type: 1,
                area: '1600px',
                offset: '65px',
                title: title,
                content: $('#orderBatchLoadSimpleForm').html(),
                success: function () {
                    if (data){
                        var option = $("form select[name='type']").find("option");
                        layui.each(option, function(index, item){
                            if(data.type  == item.text){
                                data.type = item.value;
                            }
                        });
                    }




                    admin.req('enterprisecab/cabinet/admin/cabinet/list', {cabinetNature:'sale',limit:999,cabinetType:'spring'}, function (res) {

                        if (res.code == 200) {
                            var html='<option value="">请选择售刀柜</option>'
                            for(var k=0;k<res.data.length;k++){
                                html+='<option value="'+res.data[k].id+'" >'+res.data[k].code+'</option>'
                            }
                            $("#cabId4BatchSimple").html(html);
                            form.render('select');
                            if(data){
                                $("#cabId4BatchSimple").val(data.cabId);
                                form.render('select');
                                // getCabSpace(data.cabId);
                            }
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GEt');

                    var j=0;
                    var productHtml='';
                    var getProduct=function(){
                        // var receiveId=$("#receiveId").val();

                        admin.req('enterprise/product/admin/productDetail/list', {limit:999}, function (res) {

                            if (res.code == 200) {
                                var html='<option value="">请选择物料</option>'
                                for(var k=0;k<res.data.length;k++){
                                    html+='<option value="'+res.data[k].id+'" data-skucode="'+res.data[k].skuCode+'">'+'物料编号:'+res.data[k].skuCode+'|物料名称:'+res.data[k].skuName+'|规格:'+res.data[k].oldSpecCode+'</option>'
                                }
                                productHtml=html;
                                $("#skuId4simple").html(html);
                                form.render('select');
                                if(data){
                                    $("#skuId4simple").val(data.skuId);
                                    form.render('select');
                                    // getCabSpace(data.cabId,data.skuCode);
                                }
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GEt',false);
                    }

                    var packHtml='';

                    admin.req('enterprise/product/admin/productPack/list', {limit:999,sort:'id',order:'asc'}, function (res) {

                        if (res.code == 200) {
                            var html=''
                            for(var k=0;k<res.data.length;k++){
                                html+='<option value="'+res.data[k].id+'" data-unitname="'+res.data[k].productUnitName+'" data-quantitypackunit="'+res.data[k].baseRate+'">'+res.data[k].name+'</option>'
                            }
                            packHtml=html;
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GEt',false);


                    getProduct();

                    var getCabSpace=function(cabinetId,skuCode){

                        var queryFlag='loadSimple';
                        if(data&&data.eventType=='detail'){
                            queryFlag='loadSimple4Detail';
                        }
                        admin.req('enterprisecab/cabinet/admin/cabinetCargoLane/list', {cabinetId:cabinetId,skuCode:skuCode,limit:999,queryFlag:queryFlag}, function (res) {

                            if (res.code == 200) {
                                var html='<option value="">请选择货道</option>'
                                for(var k=0;k<res.data.length;k++){
                                    html+='<option value="'+res.data[k].id+'" data-cabcode="'+res.data[k].cabCode+'" data-skucode="'+res.data[k].skuCode+'" data-skuid="'+res.data[k].skuId+'" data-storagecapacity="'+res.data[k].storageCapacity+'">'+res.data[k].code+'</option>'
                                }
                                $("#spaceId4simple").html(html);
                                form.render('select');
                                if(data){
                                    $("#spaceId4simple").val(data.spaceId);
                                    form.render('select');

                                    // var skuCode=$("#spaceId option:selected").data('skucode');
                                    // var skuId=$("#spaceId option:selected").data('skuid');
                                    // var capacity=$("#spaceId4simple option:selected").data('storagecapacity')
                                    // if (skuCode!=''&&skuCode!=null){
                                    //     var html='<option value="'+skuId+'" >'+skuCode+'</option>'
                                    //     $("#skuId").html(html);
                                    //     form.render('select');
                                    //     $("#skuId").val(data.skuId);
                                    //     form.render('select');
                                    // }else{
                                    //     getProduct(data.spaceId);
                                    // }
                                }
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GEt');
                    }

                    form.on('select(cabId)',function(selectData){
                        var cabCode=$(this).find("option:selected").text();
                        if(selectData.value!=''){
                            // getCabSpace(selectData.value);
                            $("form select[name='skuId4BatchLoad']").each(function(index,item){
                                var id=item.id;

                                var eleNo=id.substring('skuId4BatchLoad'.length,id.length);
                                var skuId=$(this).val();
                                var skuCode=$(this).find('option:selected').data('skucode');
                                var cabLaneId=$("#cabLaneId"+eleNo).val();
                                $("#quantity"+eleNo).attr('placeholder','请输入数量');
                                var quantityPackUnit=$("#packId4BatchLoad"+eleNo+" option:selected").data('quantitypackunit')
                                form.render();
                                selectcabLane(skuCode,eleNo,undefined,quantityPackUnit);

                            })

                        }
                    })





                    // form.on('select(skuId)',function (selectData) {
                    //     var cabId=$("#cabId4simple").val();
                    //     var skuCode=$("#skuId4simple option:selected").text();
                    //     if(selectData.value!=''){
                    //         getCabSpace(cabId,skuCode);
                    //     }
                    // })

                    form.on('select(spaceId)',function(selectData){

                        // var skuCode=$("#spaceId option:selected").data('skucode');
                        // var skuId=$("#spaceId option:selected").data('skuid');
                        var capacity=$("#spaceId4simple option:selected").data('storagecapacity')
                        $("form input[name='quantity']").attr('placeholder','货道最大可装填容量：'+capacity);
                        // if(selectData.value!=''){
                        //     if (skuCode!=''&&skuCode!=null){
                        //         var html='<option value="'+skuId+'" >'+skuCode+'</option>'
                        //         $("#skuId").html(html);
                        //         form.render('select');
                        //     }else{
                        //         getProduct(selectData.value);
                        //     }
                        //
                        // }
                    })

                    if(data&&data.eventType=='detail'){
                        $(".saveSimpleBtn").find('*').hide();
                        $(".model-form").find('*').attr('disabled','disabled');
                        $(".model-form input").attr('readonly',true);
                        $(".model-form select").attr('readonly',true);
                    }

                    var selectcabLane=function(skuCode,j,cabLaneId,quantityPackUnit){
                        var queryFlag='loadSimple';
                        if(data&&data.eventType=='detail'){
                            queryFlag='loadSimple4Detail';
                        }
                        var cabinetId=$("#cabId4BatchSimple").val();
                        if (cabinetId==undefined||cabinetId==''){
                            $("#cabLaneId"+j).html('<option value="">请选择货道</option>');
                            return;
                        }
                        admin.req('enterprisecab/cabinet/admin/cabinetCargoLane/list', {cabinetId:cabinetId,skuCode:skuCode,quantityPackUnit:quantityPackUnit,limit:999,queryFlag:queryFlag}, function (res) {
                            if ( res.code==200) {
                                var html='<option value="">请选择货道</option>'
                                for(var k=0;k<res.data.length;k++){
                                    html+='<option value="'+res.data[k].id+'" data-cabcode="'+res.data[k].cabCode+'" data-skucode="'+res.data[k].skuCode+'" data-skuid="'+res.data[k].skuId+'" data-storagecapacity="'+res.data[k].storageCapacity+'">'+res.data[k].code+'</option>'
                                }
                                $("#cabLaneId"+j).html(html);
                                form.render('select');
                                if(cabLaneId){
                                    $("#cabLaneId"+j).val(cabLaneId);
                                    form.render('select');

                                }

                            } else {
                                layer.msg('获取货道失败', {icon: 2, time: 1000});
                            }
                        }, 'GET');
                    }

                    var tHeaderHtml='<thead id="cabLaneHead">' +
                        '<tr>' +
                        // '<th>序号</th>' +
                        '<th style="width: 50%;">物料</th>' +
                        '<th style="width: 10%;">包装规格</th>' +
                        '<th style="width: 10%;">货道</th>' +
                        '<th style="width: 20%;">数量</th>' ;
                    if(data==undefined||(data&&data.eventType!='audit'&&data.eventType!='detail')){
                        tHeaderHtml+='<th><div align="center">操作</div></th>' ;
                    }

                    tHeaderHtml+='</tr>' +
                        '</thead>';
                    var html=tHeaderHtml+"<tbody id='cabLaneTbody'></tbody>";
                    $("#cabLaneTable").html(html);
                    form.render();

                    var getProductHtml=function(j){
                        var html='<select id="skuId4BatchLoad'+j+'"  name="skuId4BatchLoad" lay-filter="skuId4BatchLoad'+j+'" lay-verify="required" lay-search >' +
                            productHtml+
                            '</select>' ;
                        return html;
                    }

                    var getPackHtml=function(j){
                        var html='<select id="packId4BatchLoad'+j+'"  name="packId4BatchLoad" lay-filter="packId4BatchLoad'+j+'" lay-verify="required" lay-search >' +
                            packHtml+
                            '</select>' ;
                        return html;
                    }

                    var getCabLaneHtml=function(j){
                        var html= '                <select id="cabLaneId'+j+'"  name="cabLaneId" lay-filter="cabLaneId'+j+'"  lay-verify="required" lay-search >\n' +
                            '                    <option value=""> 请选择货道</option>\n' +
                            '                </select>\n' ;
                        return html;
                    }


                    var getQuantityHtml=function(j){
                        var html='<input id="quantity'+j+'" name="quantity" placeholder="请输入数量" type="text" class="layui-input" lay-verify="required|number" required/>' ;
                        return html;
                    }


                    var addTableTr=function(resultData){

                        var tBodyHtml='<tr class="layui-form cabLaneTr" id="cabLaneTr'+j+'">' +
                            // '<td>'+(j+1)+'</td>' +
                            '<td table-layout: fixed" id="skuId4BatchLoadTd'+j+'" class="skuId4BatchLoadTd">'+getProductHtml(j)+'</td>' +
                            '<td table-layout: fixed" id="packTd'+j+'">'+getPackHtml(j)+'</td>' +
                            '<td table-layout: fixed" id="cabLaneTd'+j+'"></td>' +

                            '<td id="quantityTd'+j+'">'+getQuantityHtml(j)+'</td>';
                        if(data==undefined||(data&&data.eventType!='audit'&&data.eventType!='detail')){
                            tBodyHtml+='<td style="width: 50px;table-layout: fixed" class="layui-table-cell"  align="center">' +
                                '<button id="delBtn' + j + '" class="layui-btn layui-btn-xs delBtn" type="button" lay-filter="delBtn' + j + '"  formnovalidate>删除</button>'+
                                '</td>';
                        }

                        tBodyHtml+='</tr>' ;

                        var $tr=$("#cabLaneTbody tr")
                        if($tr.length==0){
                            $("#cabLaneTbody").html(tBodyHtml);
                        }else{
                            $($tr[$tr.length-1]).after(tBodyHtml)
                            // $tr[$tr.length-1].after(tBodyHtml);
                            form.render();
                        }
                        form.render();

                        form.on('select(skuId4BatchLoad'+j+')',function(selectData){
                            var selectDom=$(this);
                            var parentDom=selectDom.parent().parent().parent();
                            var selectHtml=parentDom[0].innerHTML;
                            var startIndex=selectHtml.indexOf('skuId4BatchLoad',0);
                            var endIndex=selectHtml.indexOf('"',startIndex);
                            var selectDomNo=selectHtml.substring(startIndex,endIndex);
                            var selectDomNo=selectDomNo.substring("skuId4BatchLoad".length,selectDomNo.length);
                            var quantityPackUnit=$("#packId4BatchLoad"+selectDomNo+" option:selected").data('quantitypackunit');
                            if(selectData.value!=''&&quantityPackUnit!=''){

                                form.render('select')


                                $("#cabLaneTd"+selectDomNo).html(getCabLaneHtml(selectDomNo));

                                // $("#quantityTd"+selectDomNo).html('')
                                form.render('select');
                                var obj=$(this).find('option:selected');
                                // var skuCode=$(this).find('option:selected').data('skucode')
                                // var skuCode=$(this).find('option:selected').data('skucode');
                                var skuCode=$("#skuId4BatchLoad"+selectDomNo+" option:selected").data('skucode')
                                selectcabLane(skuCode,selectDomNo,undefined,quantityPackUnit);
                                /**
                                form.on('select(cabLaneId'+(selectDomNo)+')',function(data){
                                    if(data.value!=''){
                                        var selectDom=$(this);
                                        var parentDom=selectDom.parent().parent().parent();
                                        var selectHtml=parentDom[0].innerHTML;
                                        var startIndex=selectHtml.indexOf('cabLaneId',0);
                                        var endIndex=selectHtml.indexOf('"',startIndex);
                                        var selectDomNo=selectHtml.substring(startIndex,endIndex);
                                        var selectDomNo=selectDomNo.substring("cabLaneId".length,selectDomNo.length);
                                        var capacity=$("#cabLaneId"+selectDomNo+" option:selected").data('storagecapacity')
                                        $("#quantity"+selectDomNo).attr('placeholder','货道最大可装填容量：'+capacity);
                                    }
                                })*/
                            }

                        })

                        form.on('select(packId4BatchLoad'+j+')',function(selectData){
                            var selectDom=$(this);
                            var parentDom=selectDom.parent().parent().parent();
                            var selectHtml=parentDom[0].innerHTML;
                            var startIndex=selectHtml.indexOf('packId4BatchLoad',0);
                            var endIndex=selectHtml.indexOf('"',startIndex);
                            var selectDomNo=selectHtml.substring(startIndex,endIndex);
                            var selectDomNo=selectDomNo.substring("packId4BatchLoad".length,selectDomNo.length);
                            var skuCode=$("#skuId4BatchLoad"+selectDomNo+" option:selected").data('skucode')
                            var quantityPackUnit=$("#packId4BatchLoad"+selectDomNo+" option:selected").data('quantitypackunit');
                            if(skuCode!=''&&quantityPackUnit!=''){

                                form.render('select')


                                $("#cabLaneTd"+selectDomNo).html(getCabLaneHtml(selectDomNo));

                                // $("#quantityTd"+selectDomNo).html('')
                                form.render('select');
                                var obj=$(this).find('option:selected');
                                // var skuCode=$(this).find('option:selected').data('skucode')
                                // var skuCode=$(this).find('option:selected').data('skucode');
                                var skuCode=$("#skuId4BatchLoad"+selectDomNo+" option:selected").data('skucode')
                                selectcabLane(skuCode,selectDomNo,undefined,quantityPackUnit);
                                /**
                                 form.on('select(cabLaneId'+(selectDomNo)+')',function(data){
                                    if(data.value!=''){
                                        var selectDom=$(this);
                                        var parentDom=selectDom.parent().parent().parent();
                                        var selectHtml=parentDom[0].innerHTML;
                                        var startIndex=selectHtml.indexOf('cabLaneId',0);
                                        var endIndex=selectHtml.indexOf('"',startIndex);
                                        var selectDomNo=selectHtml.substring(startIndex,endIndex);
                                        var selectDomNo=selectDomNo.substring("cabLaneId".length,selectDomNo.length);
                                        var capacity=$("#cabLaneId"+selectDomNo+" option:selected").data('storagecapacity')
                                        $("#quantity"+selectDomNo).attr('placeholder','货道最大可装填容量：'+capacity);
                                    }
                                })*/
                            }

                        })


                        form.on('select(cabLaneId'+j+')',function(data){
                            if(data.value!=''){
                                var selectDom=$(this);
                                var parentDom=selectDom.parent().parent().parent();
                                var selectHtml=parentDom[0].innerHTML;
                                var startIndex=selectHtml.indexOf('cabLaneId',0);
                                var endIndex=selectHtml.indexOf('"',startIndex);
                                var selectDomNo=selectHtml.substring(startIndex,endIndex);
                                var selectDomNo=selectDomNo.substring("cabLaneId".length,selectDomNo.length);
                                var capacity=$("#cabLaneId"+selectDomNo+" option:selected").data('storagecapacity')
                                $("#quantity"+selectDomNo).attr('placeholder','货道最大可装填容量：'+capacity);
                            }
                        })

                        j++;
                        $(".delBtn").off('click').on('click',function(data){
                            var btn=$(this);
                            var btnId=btn[0].id;
                            var btnNo=btnId.substring("delBtn".length,btnId.length);

                            layer.confirm('确定要删除吗？', {
                                offset: '65px',
                                skin: 'layui-layer-admin'
                            }, function (i) {
                                layer.close(i);
                                btn.parent().parent().remove()
                                // layer.load(2);

                            });
                        })

                    }

                    var getLoadDetail=function(){
                        admin.req('enterprisecab/order/admin/orderReceiveLoadDetail/list', {id:data.id,loadId:data.loadId,limit:9999}, function (res) {
                            console.log(data);
                            if (res.code==200) {
                                for(let k=0;k<res.data.length;k++){
                                    addTableTr();
                                    var selectDomNo=j-1;
                                    var skuId=res.data[k].skuId;
                                    var skuCode=res.data[k].skuCode;
                                    var spaceId=res.data[k].spaceId;
                                    var quantity=res.data[k].quantity;
                                    var packId=res.data[k].packId;

                                    $("#skuId4BatchLoad"+selectDomNo).val(skuId);
                                    $("#quantity"+selectDomNo).val(quantity);
                                    $("#packId4BatchLoad"+selectDomNo).val(packId);
                                    form.render('select')
                                    $("#cabLaneTd"+selectDomNo).html(getCabLaneHtml(selectDomNo));
                                    form.render('select')
                                    selectcabLane(skuCode,selectDomNo,spaceId)

                                }
                                if(data&&(data.eventType=='audit'||data.eventType=='detail')){
                                    $("#cabLaneTbody").find("*").attr('disabled','disabled');
                                }
                            } else {
                                layer.msg('获取明细失败', {icon: 2, time: 1000});
                            }
                        }, 'GET');
                    }

                    $("#cabLaneBtnAdd").click(addTableTr);
                    if(data==undefined){
                        addTableTr();
                    }else{
                        getLoadDetail();
                        $(".saveBatchSimpleBtn").hide();
                        $("#cabLaneBtnAddDiv").hide();
                    }



                    form.val('orderLoadSimpleForm', data);
                    // 表单提交事件
                    form.on('submit(orderBatchLoadSimpleFormSubmit)', function (d) {
                        // layer.load(2);
                        var detailList=[];
                        var detailMap=new Map();
                        var spaceSkuMap=new Map();
                        var cabId=d.field.cabId;
                        d.field.cabCode=$("#cabId4BatchSimple option:selected").text();
                        $("form select[name='skuId4BatchLoad']").each(function(index,item){
                            var id=item.id;
                            // var id2=this.id;
                            var eleNo=id.substring('skuId4BatchLoad'.length,id.length);
                            var skuId=$(this).val();
                            var skuCode=$(this).find('option:selected').data('skucode');
                            var cabLaneId=$("#cabLaneId"+eleNo).val();
                            var cabLaneCode=$("#cabLaneId"+eleNo+" option:selected").text();
                            var quantity=$("#quantity"+eleNo).val();
                            var capacity=$("#cabLaneId"+eleNo+" option:selected").data('storagecapacity')
                            var quantityPackUnit=$("#packId4BatchLoad"+eleNo+" option:selected").data('quantitypackunit');
                            var unitName=$("#packId4BatchLoad"+eleNo+" option:selected").data('unitname');
                            var packId=$("#packId4BatchLoad"+eleNo).val();
                            var packName=$("#packId4BatchLoad"+eleNo+" option:selected").text();
                            detailList.push({skuCode:skuCode,skuId:skuId,spaceId:cabLaneId,spaceCode:cabLaneCode,quantity:quantity,cabId:d.field.cabId,cabCode:d.field.cabCode,quantityPackUnit:quantityPackUnit,unitName:unitName,packId:packId,packName:packName})
                            var key=cabId+'-'+cabLaneId;
                            var val=detailMap.get(key);
                            if(val){
                                val.quantity=val.quantity+parseInt(quantity);
                                detailMap.set(key,val)
                            }else{
                                val={quantity:parseInt(quantity),capacity:parseInt(capacity),spaceCode:cabLaneCode}
                                detailMap.set(key,val);
                            }
                            var val2=spaceSkuMap.get(cabLaneCode);
                            if(val2){
                                val2.push(skuCode);
                                spaceSkuMap.set(cabLaneCode,val2);
                            }else{
                                spaceSkuMap.set(cabLaneCode,[skuCode]);
                            }

                        })
                        if(detailList.length==0){
                            layer.msg('请增加上货明细', {icon: 6, time: 2000});
                            return false;
                        }
                        var invalidData;
                        var keys=[...detailMap.keys()]
                        keys.forEach(item=>{
                            var val=detailMap.get(item)
                            if(val.quantity>val.capacity){
                                invalidData=val;
                            }
                        })
                        if(invalidData){
                            layer.msg('货道['+invalidData.spaceCode+']上货数量['+invalidData.quantity+']超过货道容量['+invalidData.capacity+']', {icon: 6, time: 2000});
                            return false;
                        }

                        var keys2=[...spaceSkuMap.keys()]
                        var spaceCode
                        var skuCodes=''
                        keys2.forEach(item=>{
                            var val=spaceSkuMap.get(item);
                            if(val.length>1){
                                spaceCode=item;
                                val.forEach(item=>{
                                    skuCodes+=item+',';
                                })
                            }
                        })
                        if(spaceCode){
                            layer.msg('货道['+spaceCode+']不能同时上货不同的物料,物料编号['+skuCodes.substr(0,skuCodes.length-1)+']', {icon: 6, time: 2000});
                            return false;
                        }

                        d.field.detailList=detailList;
                        // d.field.spaceCode=$("#spaceId4simple option:selected").text();
                        // d.field.spaceCode=$("#skuId4simple option:selected").text();
                        admin.req(data ? 'enterprisecab/order/admin/orderReceiveLoad/update' : 'enterprisecab/order/admin/orderReceiveLoad/batchLoadSimple', JSON.stringify(d.field), function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('orderLoadTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST',true,'application/json; charset=utf-8');
                        return false;
                    });
                }
            });
        }

        //格子柜上货
        function showCellCabinetModel(data) {
            var title='';
            if(data==undefined){
                title='添加格子柜上货';
            }else if(data&&data.eventType=='detail'){
                title='添加格子柜上货详情';
            }
            admin.open({
                type: 1,
                area: '900px',
                offset: '65px',
                title: title,
                content: $('#cellCabinetForm').html(),
                success: function () {
                    if (data){
                        var option = $("form select[name='type']").find("option");
                        layui.each(option, function(index, item){
                            if(data.type  == item.text){
                                data.type = item.value;
                            }
                        });
                    }

                    admin.req('enterprisecab/cabinet/admin/cabinet/list', {cabinetNature:'live',limit:999}, function (res) {
                        if (res.code == 200) {
                            var html='<option value="">请选择存刀柜</option>'
                            for(var k=0;k<res.data.length;k++){
                                html+='<option value="'+res.data[k].id+'" >'+res.data[k].code+'</option>'
                            }
                            $("#cellCabId").html(html);
                            form.render('select');
                            if(data){
                                $("#cellCabId").val(data.cabId);
                                form.render('select');
                                getCabSpace(data.cabId);
                            }
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GEt');

                    var getProduct=function(){
                        admin.req('enterprise/product/admin/productDetail/list', {limit:999}, function (res) {
                            if (res.code == 200) {
                                var html='<option value="">请选择物料</option>'
                                for(var k=0;k<res.data.length;k++){
                                    html+='<option value="'+res.data[k].id+'"data-skucode="'+res.data[k].skuCode+'">'+'物料编号:'+res.data[k].skuCode+'|物料名称:'+res.data[k].skuName+'|规格:'+res.data[k].oldSpecCode+'</option>'
                                }
                                $("#productId").html(html);
                                form.render('select');
                                if(data){
                                    $("#productId").val(data.skuId);
                                    form.render('select');
                                }
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GEt');
                    }

                    getProduct();

                    admin.req('enterprise/product/admin/productPack/list', {limit:999,sort:'id',order:'asc'}, function (res) {

                        if (res.code == 200) {
                            var html=''
                            for(var k=0;k<res.data.length;k++){
                                html+='<option value="'+res.data[k].id+'" data-unitname="'+res.data[k].productUnitName+'" data-quantitypackunit="'+res.data[k].baseRate+'">'+res.data[k].name+'</option>'
                            }
                            $("#packId4Space").html(html);
                            form.render('select');
                            if(data){
                                $("#packId4Space").val(data.packId);
                                form.render('select');
                                // getCabSpace(data.cabId,data.skuCode);
                            }
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GEt');

                    var getCabSpace=function(cabinetId,skuCode,quantityPackUnit){
                        var queryFlag='loadSimple';
                        if(data&&data.eventType=='detail'){
                            queryFlag='loadSimple4Detail';
                        }
                        admin.req('enterprisecab/cabinet/admin/cabinetCargoSpace/cellListLoad', {cabId:cabinetId,skuCode:skuCode,quantityPackUnit:quantityPackUnit,limit:999,queryFlag:queryFlag}, function (res) {
                            if (res.code == 200) {
                                console.log('res.data;',res.data)
                                var html='<option value="">请选择货道</option>'
                                for(var k=0;k<res.data.length;k++){
                                    html+='<option value="'+res.data[k].id+'" data-cabcode="'+res.data[k].cabCode+'" data-skucode="'+res.data[k].skuCode+'" data-skuid="'+res.data[k].skuId+'" data-storagecapacity="'+res.data[k].storageCapacity+'">'+res.data[k].code+'</option>'
                                }
                                $("#cellSpaceId").html(html);
                                form.render('select');
                                if(data){
                                    $("#cellSpaceId").val(data.spaceId);
                                    form.render('select');
                                }
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GEt');
                    }

                    // form.on('select(cabId)',function(selectData){
                    //     var cabCode=$(this).find("option:selected").text();
                    //     if(selectData.value!=''){
                    //         getCabSpace(selectData.value);
                    //     }
                    // })

                    form.on('select(skuId)',function (selectData) {
                        var cabId=$("#cellCabId").val();
                        var skuCode=$("#productId option:selected").data('skucode');
                        var quantityPackUnit=$("#packId4Space option:selected").data('quantitypackunit');
                        if(selectData.value!=''&&skuCode!=''&&quantityPackUnit!=''){
                            getCabSpace(cabId,skuCode,quantityPackUnit);
                        }
                    })

                    form.on('select(cellCabId)',function (selectData) {
                        var cabId=$("#cellCabId").val();
                        var skuCode=$("#productId option:selected").data('skucode');
                        var quantityPackUnit=$("#packId4Space option:selected").data('quantitypackunit');
                        if(selectData.value!=''&&skuCode!=''&&quantityPackUnit!=''){
                            getCabSpace(cabId,skuCode,quantityPackUnit);
                        }
                    })

                    form.on('select(packId)',function (selectData) {
                        var cabId=$("#cellCabId").val();
                        var skuCode=$("#productId option:selected").data('skucode');
                        var quantityPackUnit=$("#packId4Space option:selected").data('quantitypackunit');
                        if(selectData.value!=''&&skuCode!=''&&quantityPackUnit!=''&&cabId!=''){
                            getCabSpace(cabId,skuCode,quantityPackUnit);
                        }
                    })

                    form.on('select(spaceId)',function(selectData){
                        console.log("12:",$("#cellSpaceId option:selected").data())
                        var capacity=$("#cellSpaceId option:selected").data('storagecapacity')
                        $("form input[name='quantity']").attr('placeholder','货道最大可装填容量：'+capacity);
                    })

                    if(data&&data.eventType=='detail'){
                        $(".saveSimpleBtn").find('*').hide();
                        $(".model-form").find('*').attr('disabled','disabled');
                        $(".model-form input").attr('readonly',true);
                        $(".model-form select").attr('readonly',true);
                    }

                    form.val('cellCabinetForm', data);
                    // 表单提交事件
                    form.on('submit(cellCabinetFormSubmit)', function (d) {
                        layer.load(2);
                        d.field.cabCode=$("#cellCabId option:selected").text();
                        d.field.spaceCode=$("#cellSpaceId option:selected").text();
                        d.field.skuCode=$("#productId option:selected").data('skucode');
                        d.field.quantityPackUnit=$("#packId4Space option:selected").data('quantitypackunit');
                        d.field.packName=$("#packId4Space option:selected").text();
                        d.field.unitName=$("#packId4Space option:selected").data('unitname');
                        admin.req(data ? 'enterprisecab/order/admin/orderReceiveLoad/update' : 'enterprisecab/order/admin/orderReceiveLoad/cellLoadSimple', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('orderLoadTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        //格子柜下货
        function showEditCellCabinetUnloadSimpleModel(data) {
            var title='添加格子柜下货';
            if(data){
                title='格子柜下货详情'
            }
            admin.open({
                type: 1,
                area: '700px',
                offset: '65px',
                title: title,
                content: $('#cellCabinetUnloadForm').html(),
                success: function () {
                    if (data){
                        var option = $("form select[name='type']").find("option");
                        layui.each(option, function(index, item){
                            if(data.type  == item.text){
                                data.type = item.value;
                            }
                        });
                    }
                    admin.req('enterprisecab/cabinet/admin/cabinet/list', {cabinetNature:'live',limit:999}, function (res) {
                        if (res.code == 200) {
                            var html='<option value="">请选择存刀柜</option>'
                            for(var k=0;k<res.data.length;k++){
                                html+='<option value="'+res.data[k].id+'" >'+res.data[k].code+'</option>'
                            }
                            $("#cabIdUnload").html(html);
                            form.render('select');
                            if(data){
                                $("#cabIdUnload").val(data.cabId);
                                form.render('select');
                                getCabSpace(data.cabId);
                            }
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GEt');
                    var getCabSpace=function(cabinetId,skuCode){
                        var queryFlag='unloadSimple';
                        if(data&&data.eventType=='detail'){
                            queryFlag='unloadSimple4Detail';
                        }
                        admin.req('enterprisecab/cabinet/app/cabinetCargoSpace/list', {cabinetId:cabinetId,limit:999,queryFlag:queryFlag}, function (res) {
                            if (res.code == 200) {
                                var html='<option value="">请选择货道</option>'
                                for(var k=0;k<res.data.length;k++){
                                    html+='<option value="'+res.data[k].id+'" data-cabcode="'+res.data[k].cabCode+'" data-skucode="'+res.data[k].skuCode+'" data-skuid="'+res.data[k].skuId+'" data-storagecapacity="'+res.data[k].storageCapacity+'" data-currentstock="'+res.data[k].currentStockCount+'">'+res.data[k].code+'</option>'
                                }
                                $("#spaceIdUnload").html(html);
                                form.render('select');
                                if(data){
                                    $("#spaceIdUnload").val(data.spaceId);
                                    form.render('select');
                                }
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GEt');
                    }

                    form.on('select(cabId)',function(selectData){
                        var cabCode=$(this).find("option:selected").text();
                        if(selectData.value!=''){
                            getCabSpace(selectData.value);
                        }
                    })
                    form.on('select(spaceId)',function(selectData){
                        var capacity=$("#spaceIdUnload option:selected").data('currentstock')
                        $("form input[name='quantity']").attr('placeholder','货道最大可下货数量：'+capacity);
                    })

                    if(data&&data.eventType=='detail'){
                        $(".saveUnloadSimpleBtn").find('*').hide();
                        $(".model-form").find('*').attr('disabled','disabled');
                        $(".model-form input").attr('readonly',true);
                        $(".model-form select").attr('readonly',true);
                    }

                    form.val('cellCabinetUnloadForm', data);
                    // 表单提交事件
                    form.on('submit(cellCabinetUnloadFormSubmit)', function (d) {
                        layer.load(2);
                        d.field.cabCode=$("#cabIdUnload option:selected").text();
                        d.field.spaceCode=$("#spaceIdUnload option:selected").text();
                        d.field.skuCode=$("#spaceIdUnload option:selected").data('skucode');
                        d.field.type='selfUnloadSimple';
                        admin.req(data ? 'enterprisecab/order/admin/orderReceiveLoad/update' : 'enterprisecab/order/admin/orderReceiveLoad/cellUnloadSimple', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('orderLoadTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }
        
        // 显示编辑弹窗
        function showEditModel(data) {
            var title='';
            if(data==undefined){
                title='添加上货记录(按计划)';
            }else if(data&&data.eventType=='detail'){
                title='添加上货记录(按计划)详情';
            }
            admin.open({
                type: 1,
                area: '700px',
                offset: '65px',
                title: title,
                content: $('#orderLoadForm').html(),
                success: function () {
                    if (data){
                        var option = $("form select[name='type']").find("option");
                        layui.each(option, function(index, item){
                            if(data.type  == item.text){
                                data.type = item.value;
                            }
                        });
                    }

                    admin.req('enterprisecab/order/admin/orderReceive/list', {limit:999}, function (res) {

                        if (res.code == 200) {
                            var html='<option value="">请选择领货单</option>'
                            for(var k=0;k<res.data.length;k++){
                                html+='<option value="'+res.data[k].id+'" data-demandcode="'+res.data[k].demandCode+'" data-loadplancode="'+res.data[k].loadPlanCode+'">'+res.data[k].code+'</option>'
                            }
                            $("#receiveId").html(html);
                            form.render('select');
                            if(data){
                                $("#receiveId").val(data.receiveId);
                                form.render('select');
                            }
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GEt');

                    form.on('select(receiveId)',function(selectData){
                        if(selectData.value!=''){
                            var demandCode=$("#receiveId option:selected").data('demandcode');
                            var loadPlanCode=$("#receiveId option:selected").data('loadplancode');
                            $("form input[name='demandCode']").val(demandCode);
                            $("form input[name='loadPlanCode']").val(loadPlanCode);
                            form.render();
                        }
                    })

                    admin.req('enterprisecab/cabinet/admin/cabinet/list', {cabinetNature:'sale',limit:999}, function (res) {

                        if (res.code == 200) {
                            var html='<option value="">请选择售刀柜</option>'
                            for(var k=0;k<res.data.length;k++){
                                html+='<option value="'+res.data[k].id+'" >'+res.data[k].code+'</option>'
                            }
                            $("#cabId").html(html);
                            form.render('select');
                            if(data){
                                $("#cabId").val(data.cabId);
                                form.render('select');
                                getCabSpace(data.cabId);
                            }
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GEt');

                    var getCabSpace=function(cabinetId){
                        var receiveId=$("#receiveId").val();
                        if(receiveId==''){
                            layer.msg('请先选择领货单', {icon: 2});
                            return;
                        }
                        var queryFlag;
                        if(data&&data.eventType!='detail'){
                            queryFlag:'receiveLoad';
                        }
                        admin.req('enterprisecab/cabinet/admin/cabinetCargoLane/list', {cabinetId:cabinetId,receiveId:receiveId,limit:999,queryFlag:queryFlag}, function (res) {

                            if (res.code == 200) {
                                var html='<option value="">请选择货道</option>'
                                for(var k=0;k<res.data.length;k++){
                                    html+='<option value="'+res.data[k].id+'" data-cabcode="'+res.data[k].cabCode+'" data-skucode="'+res.data[k].skuCode+'" data-skuid="'+res.data[k].skuId+'" data-storagecapacity="'+res.data[k].storageCapacity+'">'+res.data[k].code+'</option>'
                                }
                                $("#spaceId").html(html);
                                form.render('select');
                                if(data){
                                    $("#spaceId").val(data.spaceId);
                                    form.render('select');

                                    var skuCode=$("#spaceId option:selected").data('skucode');
                                    var skuId=$("#spaceId option:selected").data('skuid');
                                    var capacity=$("#spaceId option:selected").data('storagecapacity')
                                    if (skuCode!=''&&skuCode!=null){
                                        var html='<option value="'+skuId+'" >'+skuCode+'</option>'
                                        $("#skuId").html(html);
                                        form.render('select');
                                        $("#skuId").val(data.skuId);
                                        form.render('select');
                                    }else{
                                        getProduct(data.spaceId);
                                    }
                                }
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GEt');
                    }

                    form.on('select(cabId)',function(selectData){
                        var cabCode=$(this).find("option:selected").text();
                        if(selectData.value!=''){
                            getCabSpace(selectData.value);
                        }
                    })

                    var getProduct=function(){
                        var receiveId=$("#receiveId").val();
                        if(receiveId==''){
                            layer.msg('请先选择领货单', {icon: 2});
                            return;
                        }
                        admin.req('enterprisecab/order/admin/orderReceiveDetail/list', {receiveId:receiveId,limit:999}, function (res) {

                            if (res.code == 200) {
                                var html='<option value="">请选择物料</option>'
                                for(var k=0;k<res.data.length;k++){
                                    html+='<option value="'+res.data[k].skuId+'" >'+res.data[k].skuCode+'</option>'
                                }
                                $("#skuId").html(html);
                                form.render('select');
                                if(data){
                                    $("#skuId").val(data.skuId);
                                    form.render('select');
                                }
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GEt');
                    }

                    form.on('select(spaceId)',function(selectData){

                        var skuCode=$("#spaceId option:selected").data('skucode');
                        var skuId=$("#spaceId option:selected").data('skuid');
                        var capacity=$("#spaceId option:selected").data('storagecapacity')
                        $("form input[name='quantity']").attr('placeholder','货道最大可装填容量：'+capacity);
                        if(selectData.value!=''){
                            if (skuCode!=''&&skuCode!=null){
                                var html='<option value="'+skuId+'" >'+skuCode+'</option>'
                                $("#skuId").html(html);
                                form.render('select');
                            }else{
                                getProduct(selectData.value);
                            }

                        }
                    })

                    if(data&&data.eventType=='detail'){
                        $(".saveBtn").find('*').hide();
                        $(".model-form").find('*').attr('disabled','disabled');
                        $(".model-form input").attr('readonly',true);
                        $(".model-form select").attr('readonly',true);
                    }

                    form.val('orderLoadForm', data);
                    // 表单提交事件
                    form.on('submit(orderLoadFormSubmit)', function (d) {
                        layer.load(2);
                        d.field.cabCode=$("#cabId option:selected").text();
                        d.field.spaceCode=$("#spaceId option:selected").text();
                        d.field.skuCode=$("#skuId option:selected").text();
                        admin.req(data ? 'enterprisecab/order/admin/orderReceiveLoad/update' : 'enterprisecab/order/admin/orderReceiveLoad/load', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('orderLoadTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprisecab/order/admin/orderLoad/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        //时间范围
        laydate.render({
            elem: '#pTime',
            type: 'date',
            range: true,
            done: function (value, date, endDate) {
                if(value){
                    let dateValue = value.split(" - ");
                    $("#startTime").val(dateValue[0]+" 00:00:00");
                    $("#endTime").val(dateValue[1]+" 23:59:59");
                }else {
                    $("#startTime").val("");
                    $("#endTime").val("");
                }
                setTimeout(function() {$('[lay-filter="formSubSearchLog"]').click()}, 300);
            }
        });

        // 导出上货记录
        $('#orderLoadBtnExport').click(function () {
            var sTime = $('#startTime').val();
            var eTime = $('#endTime').val();

            if (sTime == "" && eTime == ""){
                layer.msg('请先选择上货时间！')
            } else {

                location.href = config.base + 'enterprisecab/order/admin/orderReceiveLoad/export?startCreateTime='+sTime+'&endCreateTime='+eTime+'&loadType=load';
            }
        });

    });
</script>